package com.xuecheng.auth.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.auth.dto.AuthParamsDto;
import com.xuecheng.auth.dto.XcUserExt;
import com.xuecheng.auth.entity.UserMember;
import com.xuecheng.auth.mapper.UserMemberMapper;
import com.xuecheng.auth.service.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

/**
 * ClassName: UserMemberServiceImpl
 * Package: com.xuecheng.auth.service.impl
 * Description:
 *
 * @Author 看到风
 * @Create 2024/10/22 11:42
 * @Version 1.0
 */
@Slf4j
@Component
public class UserMemberServiceImpl implements UserDetailsService {
    @Autowired
    UserMemberMapper userMemberMapper;
    @Autowired
    ApplicationContext applicationContext;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println(s);

        try {
            String decodedUsername = URLDecoder.decode(s, StandardCharsets.UTF_8.toString());
            System.out.println(decodedUsername);
            s = decodedUsername;
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        AuthParamsDto authParamsDto = null;
        try {
            authParamsDto = JSON.parseObject(s,AuthParamsDto.class);
        }catch (Exception e){
            log.info("认证请求不符合要求：{}",s);
            throw new RuntimeException("认证请求不符合要求");
        }
        //认证方法
        String authType = authParamsDto.getAuthType();
        AuthService authService = applicationContext.getBean(authType + "_authservice",AuthService.class);

        //如果不存在就返回null，然后security框架会自动返回特定的报错信息
        XcUserExt user = authService.execute(authParamsDto);
        return getUserPrincipal(user);
    }

    /**
     * @description 查询用户信息
     */
    @Autowired
    RestTemplate restTemplate;
    public UserDetails getUserPrincipal(XcUserExt user){
        //用户权限,如果不加报 Cannot pass a null GrantedAuthoritycollection

        String p = user.getIdentity().toString();
        String[] authorities = {"p1"};
        String password = user.getPassword();
        //为了安全在令牌中不放密码
        user.setPassword(null);
        //将 user 对象转 json
        String userString = JSON.toJSONString(user);
        //创建 UserDetails 对象
        UserDetails userDetails = User.withUsername(userString).password(password ).authorities(authorities).build();
        return userDetails;
    }
}

